package se.energimyndigheten.lampguiden.info;

import static se.energimyndigheten.lampguiden.info.InfoWebActivity.EXTRA_VIEW;
import se.energimyndigheten.lampguiden.R;
import se.energimyndigheten.lampguiden.SubPageListActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class InfoListActivity extends SubPageListActivity implements OnItemClickListener {

//    private static final String HELP_PATH = HTML_BASEPATH + "Help/sv_InfoList.html";
    public static final String TAG = InfoListActivity.class.getSimpleName();

    private static final Integer[] INFO_LIST = new Integer[] { R.string.info_types, R.string.info_definitions,
                    R.string.info_recycling, R.string.info_legal, R.string.info_package,
                    R.string.info_why_cancel, R.string.info_about, R.string.info_energimyndigheten };
    private static final int[] DRAWABLE_LIST = new int[] { R.drawable.ic_info_1, R.drawable.ic_info_2,
                    R.drawable.ic_info_5, R.drawable.ic_info_4, R.drawable.ic_info_6,
                    R.drawable.ic_info_3, R.drawable.ic_info_7, R.drawable.ic_info_8 };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.info_activity);
        setListAdapter(new InfoAdapter());
        getListView().setOnItemClickListener(this);
    }

    @Override
    protected void onResume() {
        mSelectedActivity = ACTIVITY_INFO;
//        super.showHelpActionItem(HELP_PATH);
        super.onResume();
    }

    class InfoAdapter extends ArrayAdapter<String> {

        public InfoAdapter() {
            super(InfoListActivity.this, R.layout.info_listitem, R.id.listitem);
            // add items
            for (int i = 0; i < INFO_LIST.length; i++) {
                add(getString(INFO_LIST[i]));
            }
            notifyDataSetChanged();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            // recycle
            View row = super.getView(position, convertView, parent);

            // inflate
            if (row == null) {
                LayoutInflater inflater = getLayoutInflater();
                row = inflater.inflate(R.layout.info_listitem, parent, false);
            }

            // update list item
            TextView listItem = (TextView) row.findViewById(R.id.listitem);
            listItem.setText(getString(INFO_LIST[position]));
            listItem.setCompoundDrawablesWithIntrinsicBounds(DRAWABLE_LIST[position], 0, 0, 0);
            listItem.setPadding(0, 0, 0, 0); // Todo: Consider altering  <style name="ListItem">

            return row;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Log.v(TAG, "onItemClick :: pos: " + position + ", id: " + id);
        Intent intent = new Intent(this, InfoWebActivity.class);

        switch (position) {
        case 0:
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
            intent.putExtra(InfoWebActivity.EXTRA_TITLE, getString(INFO_LIST[position]));
            intent.putExtra(EXTRA_VIEW, INFO_LIST[position]);
            break;
        default:
            intent = null;
        }

        if (intent != null) {
            startActivity(intent);
        }
    }
}